<template>
	<view>
		<view v-if="netWorkType!='none'">
			<view class="btn-scan iconfont" @tap="scanBind">
				&#xe62e;
			</view>
			<view><input type="text" v-model="sn" class="inp" placeholder-style="font-size:30rpx;color:#B5B5B5"
					placeholder="请输入设备后面SN码"></view>
			<view class="desc">*可通过扫描设备机身条形码，或者输入设备机身后面SN序列号进行设备绑定，区分大小写</view>
		</view>
		<view class="bind-tip " >*设备未绑定，请点击绑定</view>
		<!-- <view class="bind-tip " v-else>*已绑定设备</view> -->
		<view @tap="bindDev" class="btn  btn-bind" >
			
			绑定
		</view>
		<view class="home-box" @tap="toHome()">
			<!-- 首页 -->
				<img :src="OSS_HOST +'img/btn-home.png'" alt="">
				
		</view>

		<!-- 绑定提示窗 -->
		<uni-popup id="bindPopup" ref="bindPopup" type='center'>
			<view class="self-dialog" style="background: white;">
				<view v-if="type==1" class="iconfont padtop15" style="font-size: 100rpx; color: #19be6b;">&#xe700</view>

				<view class="title-bind text-center" :style="{color:color}">
					<text>{{title}}</text>
				</view>
				<view class="content text-center font17">
					{{content}}
				</view>
				<view>
					<view v-if="type==1" @tap="cancel" class="btn  btn-confirm" style="margin-top: 40rpx;">确定</view>
					<view v-else-if="type==2" @tap="cancel" class="btn  btn-confirm" style="margin-top: 130rpx;">确定
					</view>
					<view v-else @tap="cancel" class="btn  btn-confirm">确定</view>
				</view>
			</view>
		</uni-popup>
		<!-- 登录弹窗 -->
		<uni-popup-vlogin id="popupvLogin" ref="popupvLogin" type="bottom" />
		<!-- 提示弹框 -->
		<tui-tips ref="toast"></tui-tips>
	</view>
</template>
<script>
	import Vue from 'vue'
	import store from '@/store/index.js';
	import uniPopup from '@/components/uni-popup/uni-popup.vue'
	import {oss_host} from '@/config/config.js';
	// 公共底部
	// import btabbar from '@/components/indexBottomTable/bottomtabbar.vue'
	// import bubblePopup from '@/components/tui-bubble-popup/tui-bubble-popup.vue'
	import {
		mapGetters
	} from 'vuex';
	export default {
		components: {
			// btabbar,
			uniPopup,
			// bubblePopup
		},
		computed: {
			//注册网络状态
			...mapGetters(['netWorkType', 'loginStatus'])
		},
		data() {
			return {
				code: '',
				title: '',
				content: '',
				type: 0,
				color: '#222222',
				sn: '',
				factory:'scian',//默认scian 西恩设备   weitai：微泰血糖仪
				bindStatus:false,//绑定状态
				 OSS_HOST: oss_host,
			}
		},

		onLoad: function(e) {
			
			// console.log('onLoad: 扫码');
			if(typeof e.factory != "undefined"){
				this.factory = e.factory
			}
			
			// console.log('-------',e)
			if (typeof e.q != "undefined") {
				let scene = decodeURIComponent(e.q)
				// store.dispatch('setPromotionId', this.getQueryVariable('promotion_id', scene))
				// this.sn = this.getQueryVariable('sn', scene)
				let factory = this.getQueryVariable('factory', scene)
				if(factory ){
					this.factory = factory
				}
				// console.log(this.sn)
				// uni.setStorageSync('code_channel', code_channel);
			}

		},
		onShow() {
			// if(this.loginStatus){
			// 	this.getDev();
			// }
		},
		onUnload() {
			
		},

		methods: {
			//解析url参数
			/*
			 *  variable   参数名
			 *  url        URL
			 */
			getQueryVariable(variable, url) {
				//var url = 'https://api.aiouning.com/wechat/product/param/?sid=12&pid=37&bid=1&cid=2';
				var vars = url.split("param/?");
				// vars[1] = vars[1].replace(/%3D/g, "=").replace(/%26/g, "&")
				//console.log(vars)
				var param = vars[1].split("&");
				for (var i = 0; i < param.length; i++) {
					var pair = param[i].split("=");
					//console.log(pair)
					if (pair[0] == variable) {
						return pair[1];
					}
				}
				return (false);
			},
			getDev(){
				let _this = this
				var opt = {
						url: '/sinocareDev/getDev',
						data: {
							dev_type:3, //类型血糖
							factory:this.factory,//根据厂家，类型来判断设备是否已绑定同厂产品
						},
						success: function(res) { 
							if(res.status==1){
								_this.bindStatus =true
							}else{
								
								 _this.bindStatus =false
							}
							console.log( _this.bindStatus)
						
							 
						}
					}
					
				
				
				_this.$service.GetApi(opt)
			},
			bindPopup(type) {
				this.color = type == 2 ? 'red' : '#222222';
				this.type = type;
				this.$refs.bindPopup.open();
				switch (type) {
					case 1:
						this.title = '绑定成功'
						this.content =''
						break;
					case 2:
						this.title = '绑定失败'
						if(this.factory == 'weitai'){
							this.content = '绑定失败'
						}else{
							this.content = '绑定失败!请确认该设备是否为三诺设备'
						}
						
						break;
					case 3:
						this.title = '设备已绑定'
						this.content = '该设备已被绑定，请解除绑定后 再尝试'
						break;
				}
			},
			bindDev() {
				var _this = this
				if (!_this.loginStatus) {
					_this.$refer.setVilit('close')
					let callback = function(url) {
					
						_this.$refs.popupvLogin.open()
					
					}
					
					_this.$refer.toUrl(callback, {}, 'close')
					
					return 
				}
				if(this.sn=='' || this.sn==undefined){
					uni.showToast({
						title:'设备SN码不能为空',
						icon:'none'
					}) 
					return ;
				}
				// let sn = this.sn
				// let reg = /^a-zA-Z0-9/;
				// if (!reg.test(this.sn)) {
				// 	uni.showToast({
				// 		icon: 'none',
				// 		title: '请输入正确的设备SN码'
				// 	})
				// 	this.tel = '';
				// 	return false;
				// }

				
				
				// if(this.factory == 'scian'){
					var opt = {
						url: '/BlutdruckCheck/bindDev',
						data: {
							sn:this.sn,
							factory:this.factory,//根据厂家来判断是什么设备 
						},
						success: function(res) { 
							if(res.status==1){
								_this.bindPopup(1);
							}else{
								if(res.message=='设备已绑定'){
									 _this.bindPopup(3)
								}else{
									_this.bindPopup(2)
								}
								 
							}
							 
						}
					}
					
				// }
				// else{
				// 	var opt = {
				// 		url: '/sinocareDev/setDevBind',
				// 		data: {
				// 			sn:this.sn,
				// 			factory:this.factory,//根据厂家来判断是什么设备 
				// 		},
				// 		success: function(res) { 
				// 			if(res.status==1){
				// 				_this.bindPopup(1);
				// 			}else{
				// 				if(res.message=='设备已绑定'){
				// 					 _this.bindPopup(3)
				// 				}else{
				// 					_this.bindPopup(2)
				// 				}
								 
				// 			}
				// 			// if(res.status==1){
				// 			// 	uni.showToast({
				// 			// 		title:res.message,
				// 			// 		icon:'success',
				// 			// 		duration:2000
				// 			// 	})
				// 			// }else{
				// 			// 	uni.showToast({
				// 			// 		title:res.message,
				// 			// 		icon:'error',
				// 			// 		duration:2000
				// 			// 	})
				// 			// }
							 
				// 		}
				// 	}
					
				// }
				
				_this.$service.GetApi(opt)
			},
			toHome(){
				let _this= this
				uni.switchTab({
					url:'/pages/home/home',
					success: (res) => {
								let page = getCurrentPages().pop();
								if (page == undefined || page == null) return;
								page.curSegment = 0;
								page.onLoad();
							},
				})
			},
			//扫码绑定 
			// 根据厂家不同  绑定渠道不同  三诺 sn获取mac绑定 ，微泰 绑定sn    
			scanBind() {
				let _this = this;
				uni.scanCode({
					onlyFromCamera: true,
					success: function(res) {
						console.log(res) 
						
						if(res.scanType=='QR_CODE'){ 
							const code = res.result
							_this.sn = _this.getQueryVariable('sn', code);
							let factory = _this.getQueryVariable('factory', code);
							if(factory){
								_this.factory = factory
							}
							// let str = res.result.split('?')[1]; 
							// console.log(_this.sn)
							// let requestKey= str.substr(11); 
							// console.log(_this.factory)
							// 三诺设备绑定：根据厂家提供的二维码requestKey值和对应的sinocare_sn码表来绑定
							//sinocare_sn码表数据 由厂家提供.
							// 暂时不用了， 直接先获取自己生成的二维码直接得到sn
							// var opt = {
							// 	url: '/sinocare_dev/getDevSn',
							// 	data:{
							// 		requestKey:requestKey
							// 	},
							// 	success: function(res) { 
							// 		if(res.status==1){
							// 			 _this.sn =res.data.sn
							// 			 // _this.bindDev();
							// 		}else{
							// 			uni.showToast({
							// 				title:res.message,
							// 				icon:'none',
							// 				duration:2000
							// 			})
							// 		} 
							// 	}
							// }
							
							// _this.$service.GetApi(opt)
						 
						} 
						if(res.scanType == 'barCode'){
							 _this.sn = res.result
						}
						if(res.scanType =="CODE_128"){
							_this.sn = res.result
						}
					}
				});
			},

			cancel() {
				this.$refs.bindPopup.close();
				if(this.type==1){
					// uni.navigateBack({
					// 	delta:1
					// })
					const _this = this
					_this.$refer.setVilit('/packageHealthy/pages/dev_manage')
					let callback = function(url) {
						_this.$refs.popupvLogin.open()
					}
					let data = {
						
					}
					_this.$refer.toUrl(callback, data)
				}
			}
		}
	}
</script>

<style lang="scss">
	@import "./scan_code_bind.scss";
</style>
